import re
import time
from venv import logger

from Model.message import Message
from utils.constants import *
from wxauto import WeChat

drop_parentheses_pattern = re.compile(r"\s+\(\d+\)")


class WechatInput:

    def __init__(self, config, messageQueue):
        self.wx = WeChat()
        self.config = config
        super().__init__()
        self.messageQueue= messageQueue

    def get_contents(self):
        capture_groups = self.config[CAPTURE].keys()
        #     self.wx.AddListenChat(group_name, savepic=True, savefile=True, savevoice=True)
        while True:
            result = self.wx.GetAllNewMessage()
            if result:
                logger.info("get message: " + str(result))
            for group_name, value in result.items():
                group_name = drop_parentheses_pattern.sub('', group_name)
                if group_name in capture_groups:
                    for entry in value:
                        if entry.sender in self.config[CAPTURE][group_name]:
                            to = 'All'
                            if '@' in entry.content:
                                pass
                            message = Message(group_name, entry.sender, entry.content, None, to, time.time())
                            self.messageQueue.write_message(message)
                            logger.info("produce: " + str(message))

            time.sleep(self.config[READ_INTERVAL])